package com.bawan.vims.common.security;

import java.io.IOException;
import java.security.Security;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import com.alibaba.fastjson.JSONObject;

/**
 * <p> Description	: TripleDesHelper.java </p>
 * <p> Copyright	: Copyright (c) 2015 by PCI-SUNTEK</p>
 * <p> Author 		: ZCC </p>
 * <p> Create		: 2015年9月2日 上午10:13:44 </p> 
 * <p> version 		: 1.0 </p>
 */
public class TripleDesHelper {
	// 定义 加密算法
	private static final String ALGORITHM = "DESede";

	/**
	 * base64解码
	 */
	private static sun.misc.BASE64Decoder base64Decoder = new sun.misc.BASE64Decoder();
	/**
	 * base64解码
	 */
	private static sun.misc.BASE64Encoder base64Encoder = new sun.misc.BASE64Encoder();

	/**
	 * 3DES加密
	 * @param encryptKey
	 *            加密密钥
	 * @param encryptContent
	 *            加密明文
	 * @return
	 */
	public static String encryptMode(String encryptKey, String encryptContent) {
		try {
			SecretKey desKey = new SecretKeySpec(encryptKey.getBytes(), ALGORITHM);

			Cipher cipher = Cipher.getInstance(ALGORITHM);
			cipher.init(Cipher.ENCRYPT_MODE, desKey);
			byte[] encryptedBytes = cipher.doFinal(encryptContent.getBytes()); 
			return bytes2HexStr(encryptedBytes);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 解密
	 * @param decryptKey
	 *            解密密钥
	 * @param decryptContent
	 *            解密密文
	 * @return
	 */
	public static String decryptMode(String decryptKey, String decryptContent) {
		try {
			SecretKey desKey = new SecretKeySpec(decryptKey.getBytes(), ALGORITHM);
			Cipher cipher = Cipher.getInstance(ALGORITHM);
			cipher.init(Cipher.DECRYPT_MODE, desKey);
			byte[] decryptedBytes = cipher.doFinal(hexStr2Bytes(decryptContent));
			return new String(decryptedBytes);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	private static String bytes2HexStr(byte[] bytes) {
		return base64Encoder.encode(bytes);
	}

	private static byte[] hexStr2Bytes(String val) throws IOException {
		return base64Decoder.decodeBuffer(val);
	}

	public static void main(String[] args) {
		Security.addProvider(new com.sun.crypto.provider.SunJCE());
		// final byte[] keyBytes = {0x11, 0x22, 0x4F, 0x58, (byte) 0x88, 0x10,
		// 0x40, 0x38, 0x28, 0x25, 0x79, 0x51, (byte) 0xCB, (byte) 0xDD,
		// 0x55, 0x66, 0x77, 0x29, 0x74, (byte) 0x98, 0x30, 0x40, 0x36,
		// (byte) 0xE2}; //　24字节的密钥
		// String szSrc = "This　is　a　3DES　test";
		// System.out.println("加密前的字符串: " + szSrc);
		// byte[] encoded = encryptMode(keyBytes, szSrc.getBytes());
		// System.out.println("加密后的字符串: " + new String(encoded));
		//
		// byte[] srcBytes = decryptMode(keyBytes, encoded);
		// System.out.println("解密后的字符串: " + (new String(srcBytes)));
		// System.out.println();

		String key = "suntek_3des_012345678910";
		Map<String, String> dataMap = new HashMap<String, String>();
		dataMap.put("sender", "gansu");
		dataMap.put("command", "Command_SUNTEK_DoBuyQCoin");
		dataMap.put("telephone", "13000100100");
		dataMap.put("qqno", "10000");
		dataMap.put("buyFee", "20");
		dataMap.put("timestamp", "20150424104541");
		dataMap.put("serialId", "20150424104541000003");
		dataMap.put("verifyData", "/3v4Xy6kNOmO1dj9U9dSTHfDYHY=");
		String data = JSONObject.toJSONString(dataMap).toString();
		System.out.println("加密前的字符串: " + data);
		String encoded = encryptMode(key, data);
		System.out.println("加密后的字符串: " + encoded);

		String decoded = decryptMode(key, encoded);
		System.out.println("解密后的字符串: " + decoded);
	}
}
